package com.cib.leetcode.medium;

/**
 * 给你一个字符串 s，找到 s 中最长的回文子串
 * TODO 该算法不优秀
 * @author goomba
 **/
public class Function05 {

    public static void main(String[] args) {
        System.out.println(longestPalindrome("bb"));
    }

    public static String longestPalindrome(String s) {
        if (s == null || s.isEmpty()) return "";
        if (s.length() == 1) return s;

        String result = "";

        int leftPointer = 0;
        int rightPointer = 1;
        while (leftPointer < s.length() - 1) {

            while (rightPointer <= s.length()) {
                String subStr = s.substring(leftPointer, rightPointer);
                if (!isReverse(subStr)) {
                    rightPointer++;
                    continue;
                }

                if (subStr.length() > result.length()) {
                    result = subStr;
                }
                rightPointer++;
            }

            leftPointer++;
            rightPointer = leftPointer + 1;
        }


        return result;
    }

    private static boolean isReverse(String s) {
        int leftPointer = 0;
        int rightPointer = s.length() - 1;

        while (leftPointer < rightPointer) {
            if (s.charAt(leftPointer) != s.charAt(rightPointer)) {
                return false;
            }
            leftPointer++;
            rightPointer--;
        }
        return true;
    }


}
